import 'dart:math';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:todo_list/config/all_types.dart';
import 'package:todo_list/config/api_service.dart';
import 'package:todo_list/config/provider_config.dart';
import 'package:todo_list/i10n/localization_intl.dart';
import 'package:todo_list/model/global_model.dart';
import 'package:todo_list/utils/shared_util.dart';
import 'package:todo_list/widgets/background_slider.dart';
import 'package:todo_list/widgets/edit_dialog.dart';
import 'package:todo_list/widgets/net_loading_widget.dart';

import '../../all_page.dart';
import 'navigator_setting_page.dart';

class SettingPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final globalModel = Provider.of<GlobalModel>(context);

    return Scaffold(
      appBar: AppBar(
        title: Text(IntlLocalizations.of(context).appSetting),
      ),
      body: buildSettingListView(context, globalModel),
    );
  }
}

void onNetPicBgSelect(
    bool value, BuildContext context, GlobalModel globalModel) {
  if (value) {
    Navigator.of(context).push(new CupertinoPageRoute(builder: (ctx) {
      return ProviderConfig.getInstance().getNetPicturesPage(
        useType: NetPicturesUseType.mainPageBackground,
      );
    }));
  } else {
    globalModel.enableNetPicBgInMainPage = false;
    SharedUtil.instance.saveBoolean(Keys.enableNetPicBgInMainPage, false);
    globalModel.refresh();
  }
}

void onWeatherOpen(bool value, BuildContext context, GlobalModel globalModel) {
  if (value) {
    showDialog(
      context: context,
      builder: (ctx1) {
        return EditDialog(
          positiveWithPop: false,
          title: IntlLocalizations.of(context).enableWeatherShow,
          hintText: IntlLocalizations.of(context).inputCurrentCity,
          initialValue: globalModel.currentPosition,
          onValueChanged: (text) {
            globalModel.currentPosition = text;
          },
          sureTextStyle: TextStyle(color: globalModel.logic.getbwInDark()),
          onPositive: () {
            if (globalModel.currentPosition.isEmpty) return;
            CancelToken cancelToken = CancelToken();
            showDialog(
                context: ctx1,
                builder: (ctx2) {
                  return NetLoadingWidget(
                    onRequest: () {
                      globalModel.logic.getWeatherNow(
                          globalModel.currentPosition,
                          controller: globalModel.loadingController);
                    },
                    cancelToken: cancelToken,
                    errorText: IntlLocalizations.of(context).weatherGetWrong,
                    loadingText: IntlLocalizations.of(context).weatherGetting,
                    successText: IntlLocalizations.of(context).weatherSuccess,
                    onSuccess: () {
                      Navigator.of(context).pop();
                      Navigator.of(context).pop();
                    },
                    loadingController: globalModel.loadingController,
                  );
                });
          },
        );
      },
    );
  } else {
    globalModel.enableWeatherShow = false;
    SharedUtil.instance.saveBoolean(Keys.enableWeatherShow, false);
    globalModel.refresh();
  }
}

ListView buildSettingListView(BuildContext context, GlobalModel globalModel) {
  return ListView(
    children: <Widget>[
      // SwitchListTile(
      //   title: Text(IntlLocalizations.of(context).backgroundGradient),
      //   secondary: const Icon(
      //     Icons.invert_colors,
      //   ),
      //   value: globalModel.isBgGradient,
      //   activeColor: Theme.of(context).primaryColor,
      //   onChanged: (value) {
      //     globalModel.isBgGradient = value;
      //     SharedUtil.instance
      //         .saveBoolean(Keys.backgroundGradient, globalModel.isBgGradient);
      //     globalModel.refresh();
      //   },
      // ),
      // SwitchListTile(
      //   title: Text(IntlLocalizations.of(context).bgChangeWithCard),
      //   secondary: const Icon(
      //     Icons.format_color_fill,
      //   ),
      //   value: globalModel.isBgChangeWithCard,
      //   activeColor: Theme.of(context).primaryColor,
      //   onChanged: (value) {
      //     globalModel.isBgChangeWithCard = value;
      //     if (globalModel.isCardChangeWithBg && value) {
      //       globalModel.isCardChangeWithBg = false;
      //       SharedUtil.instance
      //           .saveBoolean(Keys.cardChangeWithBackground, false);
      //     }
      //     SharedUtil.instance.saveBoolean(
      //         Keys.backgroundChangeWithCard, globalModel.isBgChangeWithCard);
      //     globalModel.refresh();
      //   },
      // ),

      // ListTile(
      //   title: Text(IntlLocalizations.of(context).navigatorSetting),
      //   leading: Icon(
      //     Icons.navigation,
      //   ),
      //   trailing: Icon(Icons.keyboard_arrow_right),
      //   onTap: () {
      //     Navigator.of(context).push(CupertinoPageRoute(builder: (ctx) {
      //       return NavSettingPage();
      //     }));
      //   },
      // ),

      ListTile(
        title: Text("设置首页公告（40字）"),
        leading: Icon(
          Icons.info_outline,
        ),
        trailing: TextButton(onPressed: () {

        }, child: Text("确定")),
        subtitle: TextFormField(
          maxLength: 40,
          style: TextStyle(textBaseline: TextBaseline.alphabetic),
          decoration: InputDecoration(
            filled: true,
            prefixIcon: Icon(Icons.announcement),
            fillColor: Colors.transparent,
            hintText: "请输入公告",
            labelText: "请输入公告",
          ),
          obscureText: true,
        ),
        onTap: () {
          // Navigator.of(context).push(CupertinoPageRoute(builder: (ctx) {
          //   return AboutPage();
          // }));
        },
      ),

      ListTile(
        title: Text(IntlLocalizations.of(context).aboutApp),
        leading: Icon(
          Icons.info_outline,
        ),
        trailing: Icon(Icons.keyboard_arrow_right),
        onTap: () {
          // Navigator.of(context).push(CupertinoPageRoute(builder: (ctx) {
          //   return AboutPage();
          // }));
        },
      ),
    ],
  );
}
